{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "毕耜伟第五次作业"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入需要的库\n",
    "def run_question(question_number):\n",
    "    if question_number == 1:\n",
    "        question_1()\n",
    "    elif question_number == 2:\n",
    "        question_2()\n",
    "    elif question_number == 3:\n",
    "        question_3()\n",
    "    elif question_number == 4:\n",
    "        question_4()\n",
    "    elif question_number == 5:\n",
    "        question_5()\n",
    "    elif question_number == 6:\n",
    "        question_6()\n",
    "    elif question_number == 7:\n",
    "        question_7()\n",
    "    elif question_number == 8:\n",
    "        question_8()\n",
    "    elif question_number == 9:\n",
    "        question_9()\n",
    "    elif question_number == 10:\n",
    "        question_10()\n",
    "    else:\n",
    "        print(\"无效的问题编号，请输入1到10之间的数字。\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 输出9x9乘法口诀表\n",
    "def question_2():\n",
    "    for i in range(1, 10):\n",
    "        for j in range(1, i + 1):\n",
    "            print(f\"{j}*{i}={i * j}\", end=\"\\t\")\n",
    "        print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2. 输入一个任意正整数，输出分解质因数\n",
    "def question_4():\n",
    "    while True:\n",
    "        try:\n",
    "            num = int(input(\"请输入一个正整数: \"))\n",
    "            if num <= 0:\n",
    "                print(\"请输入大于0的正整数哦，请重新输入。\")\n",
    "                continue\n",
    "            print(num, \"=\", end=\" \")\n",
    "            factor = 2\n",
    "            while num > 1:\n",
    "                while num % factor == 0:\n",
    "                    print(factor, end=\"\")\n",
    "                    num //= factor\n",
    "                    if num > 1:\n",
    "                        print(\"*\", end=\"\")\n",
    "                factor += 1\n",
    "            break\n",
    "        except ValueError:\n",
    "            print(\"你输入的不是合法的整数，请重新输入。\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3. 输入一个不多于6位的正整数，求它是几位数，逆序打印出各位数字\n",
    "def question_6():\n",
    "    num = input(\"请输入一个不多于6位的正整数: \")\n",
    "    print(f\"它是{len(num)}位数\")\n",
    "    print(num[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4. 求s=a+aa+aaa+aaaa+aa...a的值，其中a是一个数字，个数由键盘控制\n",
    "def question_8():\n",
    "    a = int(input(\"请输入一个数字: \"))\n",
    "    n = int(input(\"请输入相加的个数: \"))\n",
    "    s = 0\n",
    "    t = 0\n",
    "    for _ in range(n):\n",
    "        t = t * 10 + a\n",
    "        s += t\n",
    "    print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 5. 求分数序列前N项之和，N由键盘输入\n",
    "def question_10():\n",
    "    n = int(input(\"请输入项数N: \"))\n",
    "    sum_frac = 0\n",
    "    a, b = 2, 1\n",
    "    for _ in range(n):\n",
    "        sum_frac += a / b\n",
    "        a, b = a + b, a\n",
    "    print(sum_frac)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 6. 求满足条件的整数（加上100后是一个完全平方数，再加上268又是一个完全平方数）\n",
    "def question_12():\n",
    "    for num in range(1, 100000):\n",
    "        n1 = (num + 100) ** 0.5\n",
    "        n2 = (num + 268) ** 0.5\n",
    "        if int(n1) == n1 and int(n2) == n2:\n",
    "            print(num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 7. 计算每个月的兔子总数（斐波那契数列相关兔子繁殖问题）\n",
    "def question_14():\n",
    "    while True:\n",
    "        try:\n",
    "            month = int(input(\"请输入月数: \"))\n",
    "            if month < 1:\n",
    "                print(\"月数应该是正整数，请重新输入。\")\n",
    "                continue\n",
    "            if month == 1 or month == 2:\n",
    "                print(1)\n",
    "            else:\n",
    "                f1, f2 = 1, 1\n",
    "                for _ in range(3, month + 1):\n",
    "                    f1, f2 = f2, f1 + f2\n",
    "                print(f2)\n",
    "            break\n",
    "        except ValueError:\n",
    "            print(\"你输入的不是合法的整数，请重新输入。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 8. 求任意输入的3x3矩阵对角线元素之和\n",
    "def question_16():\n",
    "    matrix = []\n",
    "    for _ in range(3):\n",
    "        row = list(map(int, input(\"请输入矩阵一行的3个数字，用空格隔开: \").split()))\n",
    "        matrix.append(row)\n",
    "    sum_diagonal = 0\n",
    "    for i in range(3):\n",
    "        sum_diagonal += matrix[i][i] + matrix[i][2 - i]\n",
    "    print(sum_diagonal)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 9. 使n个整数前面各数顺序向后移m个位置\n",
    "def question_18():\n",
    "    while True:\n",
    "        try:\n",
    "            n = int(input(\"请输入整数的个数: \"))\n",
    "            m = int(input(\"请输入后移的位数: \"))\n",
    "            if n < 1 or m < 0:\n",
    "                print(\"输入的整数个数应大于0，后移位数应大于等于0，请重新输入。\")\n",
    "                continue\n",
    "            nums = list(map(int, input(\"请依次输入整数，用空格隔开: \").split()))\n",
    "            if len(nums)!= n:\n",
    "                print(f\"输入的整数个数与你声明的个数{n}不一致，请重新输入。\")\n",
    "                continue\n",
    "            m %= n\n",
    "            nums = nums[-m:] + nums[:-m]\n",
    "            print(nums)\n",
    "            break\n",
    "        except ValueError:\n",
    "            print(\"你输入的数字格式不正确，请重新输入。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 10. 根据奇偶性求不同分式和（利用指针函数，这里简单模拟指针函数逻辑用普通函数实现）\n",
    "def question_20():\n",
    "    while True:\n",
    "        try:\n",
    "            n = int(input(\"请输入一个整数: \"))\n",
    "            sum_value = 0\n",
    "            if n % 2 == 0:\n",
    "                for i in range(2, n + 1, 2):\n",
    "                    sum_value += 1 / i\n",
    "            else:\n",
    "                for i in range(1, n + 1, 2):\n",
    "                    sum_value += 1 / i\n",
    "            print(sum_value)\n",
    "            break\n",
    "        except ValueError:\n",
    "            print(\"你输入的不是合法的整数，请重新输入。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 = 2"
     ]
    }
   ],
   "source": [
    "# 交互方式运行\n",
    "if __name__ == \"__main__\":\n",
    "    question_num = int(input(\"请输入问题编号（1-10）: \"))\n",
    "    run_question(question_num)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
